package example;

import java.util.Arrays;

/**
 * 输入一个字符串，按照ASC||码升序排列，然后根据当前字母在字母表的位置（a，A均为1）,计算平方，除以26取余数，结果加1，根据计算结果找到字母表对应的字母（大写找大写，小写找小写）
 *
 * eg：字母B，在字母表位置是2，计算过程2*2%26+1=5，字母表5对应的字母是E（大写B对应大写E）
 *
 * 测试用例：输入“abB”输出“Ebe”（升序排列后为Bab，然后计算）
 */

public class Exam001 {

    String sortLetterStr(String letterStr) {
        char[] charArray = letterStr.toCharArray();
        Arrays.sort(charArray);
        for (int i = 0; i < charArray.length; i++) {
            char temp = charArray[i];
            int order = getOrder(temp);
            int newOrder = (order * order) % 26 + 1;
            char newChar = getChar(newOrder, getUpperCase(temp));
            charArray[i] = newChar;
        }
        return new String(charArray);
    }


    private int getOrder(char cha) {
        if (cha >= 'a' && cha <= 'z') {
            return cha - 'a' + 1;
        } else if (cha >= 'A' && cha <= 'Z') {
            return cha - 'A' + 1;
        } else {
            return -1;
        }
    }

    private boolean getUpperCase(char cha) {
        return cha >= 'A' && cha <= 'Z';
    }

    private char getChar(int order, boolean upperCase) {
        if (upperCase) {
            return (char) (order + 'A' - 1);
        } else {
            return (char) (order + 'a' - 1);
        }
    }

    private char changeChar(char temp) {
        int order = getOrder(temp);
        int newOrder = (order * order) % 26 + 1;
        char newChar = getChar(newOrder, getUpperCase(temp));
        return newChar;
    }


    public static void main(String[] args) {
        Exam001 exam001 = new Exam001();
//        System.out.println(exam001.getOrder('a'));
//        System.out.println(exam001.getOrder('A'));
//        System.out.println(exam001.getOrder('z'));
//        System.out.println(exam001.getOrder('Z'));

        String result1 = exam001.sortLetterStr("abB");
        System.out.println(result1);

        String result2 = exam001.sortLetterStr("HuaweiBest");
        System.out.println(result2);

        char b = exam001.changeChar('B');
        System.out.println(b);

    }


}
